using System;
using System.Diagnostics;

namespace Oracle.ManagedDataAccess.Client
{
	internal class OraclePerfCounter
	{
		private bool m_isDisposed;

		public PerformanceCounter m_counter;

		public OraclePerfCounter m_higherLevelCounter;

		internal OraclePerfCounter(string counterName, string perfCounterInstanceName)
		{
			try
			{
				m_counter = new PerformanceCounter("ODP.NET, Core Driver", counterName, perfCounterInstanceName, readOnly: false);
			}
			catch (Exception)
			{
				m_counter = null;
			}
		}

		internal OraclePerfCounter(string counterName)
		{
			try
			{
				m_counter = new PerformanceCounter("ODP.NET, Core Driver", counterName, OraclePerfParams.m_appDomainPfcInstanceName, readOnly: false);
			}
			catch (Exception)
			{
				m_counter = null;
			}
		}

		public long IncrementBy(int value)
		{
			try
			{
				if (m_higherLevelCounter != null)
				{
					m_higherLevelCounter.IncrementBy(value);
				}
				return (m_counter != null) ? m_counter.IncrementBy(value) : (-1);
			}
			catch (Exception)
			{
				Dispose();
				return -1L;
			}
		}

		public long Increment()
		{
			try
			{
				if (m_higherLevelCounter != null)
				{
					m_higherLevelCounter.Increment();
				}
				return (m_counter != null) ? m_counter.Increment() : (-1);
			}
			catch (Exception)
			{
				Dispose();
				return -1L;
			}
		}

		public long Decrement()
		{
			try
			{
				if (m_higherLevelCounter != null)
				{
					m_higherLevelCounter.Decrement();
				}
				return (m_counter != null) ? m_counter.Decrement() : (-1);
			}
			catch (Exception)
			{
				Dispose();
				return -1L;
			}
		}

		public void Dispose()
		{
			if (!m_isDisposed)
			{
				if (m_counter != null)
				{
					m_counter.RemoveInstance();
					m_counter.Dispose();
					m_counter = null;
				}
				m_higherLevelCounter = null;
				m_isDisposed = true;
			}
		}
	}
}
